﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;

using Microsoft.Practices.Unity;

namespace ERPStore.Tests.Controllers
{
	[TestFixture]
	public class CatalogControllerTests : TestBase
	{
		[SetUp]
		public override void Initialize()
		{
			base.Initialize();
		}

		[Test]
		public void Index()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = (System.Web.Mvc.ViewResult)controller.Index();
			Assert.AreEqual(result.ViewName, string.Empty);
		}

		[Test]
		public void Product_Good_Code()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Product("xbox360", "Xbox_360_Arcade") as System.Web.Mvc.ViewResult;
			var product = result.Model as ERPStore.Models.Product;
			Assert.IsNotNull(product);
			Assert.AreEqual(product.Code, "XBOX360");
		}

		[Test]
		public void Product_Null_Code()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Product(null, null) as System.Web.Mvc.ViewResult;
			Assert.AreEqual(result.ViewName, "NoProductFound");
		}

		[Test]
		public void Product_Unkwnown_Code()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Product("badcode", null) as System.Web.Mvc.ViewResult;
			Assert.AreEqual(result.ViewName, "404");
		}

		[Test]
		public void Product_Variant()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Product("variant1", "Variant_1") as System.Web.Mvc.ViewResult;
			Assert.AreEqual(result.ViewName, "VariationProduct");
		}

		[Test]
		public void Product_Variation()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Product("variation1", "Variation_1") as System.Web.Mvc.ViewResult;
			Assert.AreEqual(result.ViewName, string.Empty);
			var product = result.Model as ERPStore.Models.Product;
			Assert.IsNotNull(product);
			Assert.AreNotEqual(product.Code, "variation1");
		}

		[Test]
		public void Search_Simple()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Search("xbox") as System.Web.Mvc.ViewResult;

			var list = result.Model as ERPStore.Models.ProductList;

			Assert.IsNotNull(list);
			Assert.AreEqual(list.Query, "xbox");
		}

		[Test]
		public void Search_Null()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Search(null) as System.Web.Mvc.ViewResult;

			Assert.IsNotNull(result.ViewName , "NoProductFound");
		}

		[Test]
		public void Search_Empty()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Search(string.Empty) as System.Web.Mvc.ViewResult;

			Assert.IsNotNull(result.ViewName, "NoProductFound");
		}

		[Test]
		public void Search_Keyword_Substitution()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Search("xox") as System.Web.Mvc.ViewResult;

			var list = result.Model as ERPStore.Models.ProductList;

			Assert.IsNotNull(list.Substitution);
			Assert.AreEqual(list.Substitution.Type, Models.ProductSearchSubstitutionType.Keyword);
		}

		[Test]
		public void Search_Variant_Substitution()
		{
			var controller = CreateController<ERPStore.Web.Controllers.CatalogController>();
			var result = controller.Search("variant1") as System.Web.Mvc.RedirectResult;

			Assert.NotNull(result.Url);
		}


	}
}
